<template>
  <div class="toast" v-show="visible">
    <div class="container">
      <div class="content">
        {{ message }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "toast",
  data() {
    return {
      message: "", // 消息文字
      duration: 2000, // 显示时长，毫秒
      closed: false, // 用来判断消息框是否关闭
      timer: null, // 计时器
      visible: false, // 是否显示
    };
  },
  mounted() {
    this.startTimer();
  },
  watch: {
    closed(newVal) {
      if (newVal) {
        this.visible = false;
        this.destroyElement();
      }
    },
  },
  methods: {
    destroyElement() {
      this.$destroy();
      this.$el.parentNode.removeChild(this.$el);
    },
    startTimer() {
      this.timer = setTimeout(() => {
        if (!this.closed) {
          this.closed = true;
          clearTimeout(this.timer);
        }
      }, this.duration);
    },
  },
};
</script>

<style scoped>
.toast {
  position: fixed;
  bottom: 64px;
  left: 0px;
  width: 100%;
  height: 25px;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  display: flex;
}
.toast .container {
  padding: 6px 11px;
  background-color: #4d4d4d;
  border-radius: 25px;
  /* box-shadow: 0px 10px 50px rgba(0, 0, 31, 0.3); */
}
.toast .container .content {
  color: rgba(255, 255, 255, 1);
  font-size: 14px;
  /* text-align: center; */
  /* letter-spacing: 3px; */
}
</style>
